<?php

/**
 * MlIncidents form.
 *
 * @package    maslogger
 * @subpackage form
 * @author     Michaël VEROUX
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class MlIncidentsForm extends BaseMlIncidentsForm {

    public function configure() {
        unset($this['created_at'], $this['updated_at'], $this['deleted_at'], $this['version'], $this['created_by'], $this['updated_by']);
        $this->disable_checkbox = false;
        // Schémas
        $this->widgetSchema['machine_id'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['version_id'] = new sfWidgetFormInputHidden();
        $this->setSuggestBox();
        /*
        $defaultDateTime = $this->getObject()->getDateTimeObject('intervention_date')->format('Y-m-d H:i');
        $dateWidget = new sfWidgetFormDateTime(array(
                    'date' => array('format' => '%day%/%month%/%year%'),
                    'format' => '%date% %time%'
                        ),
                    array('value' => $defaultDateTime));
        $this->widgetSchema['intervention_date'] = new sfWidgetFormJQueryDate(array('culture' => 'fr_FR', 'date_widget' => $dateWidget));
         
         */
        $defaultDateTime = $this->getObject()->getDateTimeObject('intervention_date')->format('Y-m-d H:i');
        $this->widgetSchema['intervention_date'] = new sfWidgetFormDateTime(    array(  'date' => array('format' => '%day%/%month%/%year%'),
                                                                                        'format' => '%date% %time%', 
                                                                                        'default' => date('Y/m/d H:i')),
                                                                                array('value' => $defaultDateTime));
        
        $this->widgetSchema['photo'] = new sfWidgetFormInputFileEditablePerso(array(
                    'label' => 'Capture photo',
                    'delete_label' => 'Effacer la photo enregistrée',
                    'file_src' => $this->getObject()->getPhoto(),
                    'is_image' => true,
                    'edit_mode' => !$this->isNew(),
                ));
        
        // Validators
        $this->validatorSchema['photo'] = new sfValidatorFile(array(
                    'required' => false,
                    'path' => sfConfig::get('sf_upload_dir') . '/captures',
                    'mime_types' => 'web_images'
                ));
        $this->validatorSchema['photo_delete'] = new sfValidatorBoolean();
        //$this->setValidator('intervention',new sfValidatorString(array('required' => true)));
    }

    public function setSuggestBox() {
        $this->widgetSchema['suggest_box'] = new sfWidgetFormJQueryAutocompleter(array(
                    'url' => "ajaxSelect",
                    'config' => '{ scrollHeight: 250 ,
                      autoFill: false }',
                    'response' => '
  var retour = jQuery.ajax({
    type: "GET",
    url: "/incident/declare?numero="+data[1],
    async: false
  }).responseText;
jQuery("#sf_admin_container").html(retour);
etiquettes();'
                ));
    }

    public function disableIntervention() {
        $this->widgetSchema['intervention'] = new sfWidgetFormTextarea(array(), array('readonly' => 'readonly'));
    }
    public function disableCheckbox(){
        if($this->getObject()->getControler()){
            $this->widgetSchema['controler']->setAttribute('disabled','disabled');
            $this->disable_checkbox[] = 'controler';
        }
        if($this->getObject()->getSfm()){
            $this->widgetSchema['sfm']->setAttribute('disabled','disabled');
            $this->disable_checkbox[] = 'sfm';
        }
    }
    public function bind(array $taintedValues = null, array $taintedFiles = null){
        if(is_array($this->disable_checkbox)){
            foreach($this->disable_checkbox AS $checkbox){
                $taintedValues[$checkbox] = $this->getObject()->get($checkbox);
            }
        }
        return parent::bind($taintedValues, $taintedFiles);
    }
}
